
let stop = { result: '-1' }; // 流程终止，不继续执行下一步
let finish = { result: '0' }; // 流程完成 (有可能是中间任意一步结束)

export default class eligClass {

    
   /**
   * 类初始化方法
   */
  constructor() {
    shouldStop: false;
    // 生成一个0到99之间的随机数
    let randomNum = Math.floor(Math.random() * 100);
 
    // 格式化数字，使其始终为三位数（若不足则前面补零）
    // 使用padStart(3, '0')方法，在数字字符串前补零直到长度为3
    this.randomNumber = randomNum.toString().padStart(3, '0');
  }

  
 checkInsideEligibility(params, business_kind ) {
    return new Promise((resolve, reject) => {
  
      let count = 0;
      const intervalId = setInterval(() => {
        count++;
        console.log('checkInsideEligibility finish', window.start_elig_biz_id);
        if (count >= 5) {
          clearInterval(intervalId);
          resolve(finish);
        }
      }, 300);
    });
  }
    
  
 checkESignAuth(params, business_kind) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('checkESignAuth finish', params, business_kind);
        resolve(finish);
      }, 3000);
    });
  }
   
   
 checkPublicFund(params, business_kind) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('checkPublicFund finish', params, business_kind);
        resolve(finish);
      }, 3000);
    });
  }
    
  
  /**
   * 所有适当性流程支持可配置
   * @param searchRes
   * @param params
   * @returns {Promise<void>}
   */
 async checkAllElig(params, business_kind, successCallback, failCallback){
    this.shouldStop = false;
    console.log("checkAllElig的params入参===", JSON.stringify(params), business_kind);
    let resultObj = {
      instr_batch_no: "",
      external_check_no:"",
      convert_bond_risk_entrust_confirm_tip:"",
      extend: {}
    }
  
  
    let functionList = 'checkInsideEligibility,checkPublicFund,checkESignAuth';



    if(!(functionList.trim())){
      successCallback && successCallback(resultObj);
      return;
    }
     let funcArray = functionList.split(",");
    
    const elementCount = {}; // 用于存储元素出现次数的对象  
  
  
    for(let i=0;i< funcArray.length; i++ ){
       let funcName = funcArray[i].trim();
  
      // 如果元素已经存在于elementCount对象中，则增加其计数  
      if (elementCount[funcName]) {  
        elementCount[funcName]++;  
      } else {  
        // 否则，初始化其计数为1  
        elementCount[funcName] = 1;  
      }  
  
      // 计算方法第几次出现
      const timeNo = elementCount[funcName]
      console.log('===============start function ======',funcName, this.shouldStop);
  
      let res = await this[funcName](params, business_kind, timeNo);
      if(res.result != '0' || this.shouldStop){
        failCallback && failCallback();
        return ;
      }else {
        if(funcName == "checkInsideEligibility"){
          resultObj.instr_batch_no = res.instr_batch_no;
        }
        if(funcName == "checkPublicFund"){
          resultObj.external_check_no = res.external_check_no;
        }
        if(funcName == "convertBondRiskTips"){
          resultObj.convert_bond_risk_entrust_confirm_tip = res.convert_bond_risk_entrust_confirm_tip;
        }
        if(res.extend){
          resultObj.extend = res.extend;
        }
      }
    }
    // console.log("resultObj===" + JSON.stringify(resultObj));
    console.log('====checkAllElig====', business_kind);
    successCallback && successCallback(resultObj);  
  }

}